import { defineConfig } from 'vite'
import terser from '@rollup/plugin-terser'
import compression from 'vite-plugin-compression';
import createCdnImportPlugin from 'vite-plugin-cdn-import';
import vue from '@vitejs/plugin-vue'

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue(), compression({
    deleteOriginFile: true,
    algorithm: 'gzip',
    compressionOptions: {
      level: 9
    },
  }), createCdnImportPlugin({
    modules: [
      {
        name: 'vue',
        var: 'Vue',
        path: 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.5.13/vue.runtime.global.prod.min.js',
      },
      {
        name: 'element-plus',
        var: 'ElementPlus',
        path: 'https://cdn.bootcdn.net/ajax/libs/element-plus/2.7.7/index.full.min.js',
      }
    ]
  })],
  resolve: {
    alias: {
      '@': '/src',
    },
  },
  server: {
    host: '0.0.0.0',
    port: 5173,
    proxy: {
      '/api': {
        target: 'https://www.paddlepaddle.org.cn/',
        changeOrigin: true,
        rewrite: (path: any) => path.replace(/^\/api/, '')
      }
    }
  },
  build: {
    rollupOptions: {
      plugins: [terser()],
      output: {
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[name]-[hash][extname]',
        manualChunks(id: string) {
            return 'npm_' + id.split('/')[0];
        }
      },
    }
  }
})
